<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/6/30
 * Time: 17:01
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Privateuser extends Base
{
    /**
     * 私人订制管理员
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $user_phone = input('user_phone');
            $user_private_rule = input('user_private_rule'); // 1 仓管  2 审核员  3 工程师
            
            $where['user_private_worker'] = 2;
            $where['user_status'] = ['between', [1, 2]];
            if(isset($user_phone) && $user_phone != '') $where['user_phone'] = ['like', "%$user_phone%"];
            if(isset($user_private_rule) && $user_private_rule != '') $where['user_private_rule'] = $user_private_rule;
            
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->where($where)->count();// 获取总条数
            $lists = Db::name('u_user')
                ->where($where)
                ->field('user_id, user_phone, user_username, user_real_name, user_private_rule, user_status, 
                user_image, user_time')
                ->page($NowPage, $limits)
                ->order('user_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加私人订制工作人员
     * @return mixed|\think\response\Json
     */
    public function userAdd(){
        if(Request::instance()->isPost()){
            $user_phone = trim(input('user_phone'));
            $user_username = input('user_username');
            $user_real_name = input('user_real_name');
            $user_password = input('user_password');
            $user_image = input('user_image');
            $user_private_rule = input('user_private_rule');
            if(!preg_match("/^1[345678]\d{9}$/", $user_phone)) return json(['code' => 100, 'msg' => '请输入正确的手机号']);
            
            $insert = [
                'user_username' => $user_username,
                'user_image' => $user_image,
                'user_phone' => $user_phone,
                'user_password' => md5($user_password),
                'user_real_name' => $user_real_name,
                'user_time' => date('Y-m-d H:i:s'),
                'user_private_worker' => 2,
                'user_is_perfect' => 2,
                'user_rule' => 2,
                'user_private_rule' => $user_private_rule
            ];
            $res = Db::table('up_u_user')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('privateuser/useradd');
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function userDetail(){
        $user_id = input('user_id');
        $user = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_phone, user_username, user_real_name, user_image, user_private_rule')
            ->find();
        return $this->fetch('privateuser/userdetail', ['user' => $user]);
    }
    
    /**
     * 编辑私人订制工作人员
     * @return mixed|\think\response\Json
     */
    public function userEdit(){
        $user_id = input('user_id');
        if(Request::instance()->isPost()){
            $user_phone = trim(input('user_phone'));
            $user_username = input('user_username');
            $user_real_name = input('user_real_name');
            $user_password = input('user_password');
            $user_image = input('user_image');
            $user_private_rule = input('user_private_rule');
            if(!preg_match("/^1[345678]\d{9}$/", $user_phone)) return json(['code' => 100, 'msg' => '请输入正确的手机号']);
            
            $update = [
                'user_username' => $user_username,
                'user_image' => $user_image,
                'user_phone' => $user_phone,
                'user_real_name' => $user_real_name,
                'user_update_time' => date('Y-m-d H:i:s'),
                'user_private_worker' => 2,
                'user_private_rule' => $user_private_rule
            ];
            if($user_password) $update['user_password'] = md5($user_password);
            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $user = Db::table('up_u_user')->where('user_id', $user_id)
            ->field('user_phone, user_username, user_real_name, user_image, user_private_rule')
            ->find();
        $this->assign([
            'user_id' => $user_id,
            'user' => $user
        ]);
        return $this->fetch('privateuser/useredit');
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function userFreeze(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function userUsing(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除私人订制工作人员
     * @return \think\response\Json
     */
    public function userDel(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}